package lore.soft.fullkeyboard;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.inputmethodservice.Keyboard;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;

import java.util.ArrayList;

public class FullKeyboard extends Keyboard {

    private Key mEnterKey;
    private Key mSpaceKey;
    private boolean mShifted;

    private Key mShiftKeyLeft;
    private Key mShiftKeyRight;
    private Key mCtrlKey;
    private Key mAltKey;

    private ArrayList<Key> mMKey;

    /**
     * Stores the current state of the mode change key. Its width will be dynamically updated to
     * match the region of {@link #mFunctionKey} when {@link #mFunctionKey} becomes invisible.
     */
    private Key mFunctionKey;
    /**
     * Stores the current state of the language switch key (a.k.a. globe key). This should be
     * visible while {@link InputMethodManager# shouldOfferSwitchingToNextInputMethod(IBinder)}
     * returns true. When this key becomes invisible, its width will be shrunk to zero.
     */
    private Key mLanguageSwitchKey;


    public FullKeyboard(Context context, int xmlLayoutResId) {
        super(context, xmlLayoutResId);
    }

    public FullKeyboard(Context context, int layoutTemplateResId,
                        CharSequence characters, int columns, int horizontalPadding) {
        super(context, layoutTemplateResId, characters, columns, horizontalPadding);
    }

    @Override
    protected Key createKeyFromXml(Resources res, Row parent, int x, int y,
                                   XmlResourceParser parser) {
        Key key = new Key(res, parent, x, y, parser);
        if (null == mMKey) {
            mMKey= new ArrayList<>();
        }
        if (key.codes[0] == KeyEvent.KEYCODE_ENTER) {
            mEnterKey = key;
        } else if (key.codes[0] == KeyEvent.KEYCODE_SPACE) {
            mSpaceKey = key;
        } else if (key.codes[0] == KeyEvent.KEYCODE_FUNCTION) {
            mFunctionKey = key;
        } else if (key.codes[0] == KeyEvent.KEYCODE_SHIFT_LEFT) {
            mShiftKeyLeft = key;
        } else if (key.codes[0] == KeyEvent.KEYCODE_SHIFT_RIGHT) {
            mShiftKeyRight = key;
        } else if (key.codes[0] == KeyEvent.KEYCODE_CTRL_LEFT) {
            mCtrlKey = key;
        } else if (key.codes[0] == KeyEvent.KEYCODE_ALT_LEFT) {
            mAltKey = key;
        } else if (key.codes[0] == KeyEvent.KEYCODE_LANGUAGE_SWITCH) {
            mLanguageSwitchKey = key;
        }else if(key.codes[0]<= res.getInteger(R.integer.KEYCODE_CUSTOM_START) &&
                key.codes[0]>= res.getInteger(R.integer.KEYCODE_CUSTOM_END)){
            mMKey.add(res.getInteger(R.integer.KEYCODE_CUSTOM_START)- key.codes[0], key);
        }
        return key;
    }

    public Key getAltKey() {
        return mAltKey;
    }

    public Key getCtrlKey() {
        return mCtrlKey;
    }

    public void setShiftLock(boolean on) {
        mShifted = on;
        if (null != mShiftKeyLeft) {
            mShiftKeyLeft.on = on;
        }
        if (null != mShiftKeyRight) {
            mShiftKeyRight.on = on;
        }
    }

    public boolean isShiftLocked() {
        return mShifted;
    }

    public boolean setCustomKey(int index, String label) {
        try{
            if (null != mMKey.get(index)) {
                mMKey.get(index).label= label;
                return true;
            }
        }catch (IndexOutOfBoundsException ioobe){

        }catch(NullPointerException npe){

        }
        return false;
    }
}
